namespace BoxPlayer
{
    class T_Box_AudioDevice; //
    class T_Box_AudioOutput; //
};

#ifndef T_BOX_AUDIO_H
#define T_BOX_AUDIO_H

#include "stable.h"
#include "t_av_packet.h"
#include "t_box_context.h"

namespace BoxPlayer
{
    class T_Box_AudioDevice : public QObject
    {
        Q_OBJECT
    protected:
        T_Box_Media *m_media;
        QAudioFormat m_format;
        QAudioOutput *m_audioOutput;
    public:
        explicit T_Box_AudioDevice(T_Box_Media *a_media);
        virtual ~T_Box_AudioDevice();
        void start(QIODevice *a_generator);
        void stop();
        void suspend();
        void resume();
        QAudioOutput *getQAudioOutput();
    };

    class T_Box_AudioOutput : public QIODevice, public T_Box_Seekable
    {
        Q_OBJECT
    protected:
        T_Box_Context *m_stream;
        T_Box_AudioDevice m_audioDevice;
        qint64 m_out_read_size;
        qint64 m_max_seek_size;
        QByteArray m_audio_data_left;
        QByteArray m_audio_last_read;
        qint64 m_base_bytes;
        qint64 m_base_msecs;
        T_AV_AudioDecodeBuffer m_decode_buff;
    public:
        explicit T_Box_AudioOutput(T_Box_Context *a_stream, QObject *parent);
        virtual ~T_Box_AudioOutput();
        virtual qint64 seekMe(T_Box_Seek_Manager *a_seek_mgr, qint64 a_msec); //[T_Box_Seekable]
        qint64 elapsed();
        void start();
        void stop();
        void suspend();
        void resume();
    protected:
        //[Generator Functions]
        virtual bool seek(qint64 pos);
        virtual qint64 readData(char *data, qint64 maxlen);
        virtual qint64 writeData(const char *data, qint64 len);
        virtual qint64 bytesAvailable() const;
    protected:
        QByteArray p_readPacket(qint64 prec_len, qint64 need_len);
        QByteArray p_pop_front(QByteArray &a_bytes, int a_maxlen);
        QByteArray p_pop_back(QByteArray &a_bytes, int a_maxlen);
    };

};
#endif // T_BOX_AUDIO_H
